<?php

    class MessageController extends Zend_Controller_Action
    {

        private $messageModel;
        private $user;

        public function init()
        {
            /*
             * User must be logged in to view messages
             * If session times out then user will be redirected to the home page
             */
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            if (empty($y))
            {

                $this->_redirect("");
            }
            else
            {
                $this->user = (array) $y;
            }

            $this->messageModel = new Application_Model_Message;
        }

        public function indexAction()
        {

            $commonModel = new Application_Model_Common();
            $friend_records = $commonModel->getAllRecordFromInnerJoin(TABLE_FRIEND_REQUESTS, 'users', 'users.user_id = friend_requests.user_sent_to', 'user_sent_from=' . $this->user['user_id'] . ' and user_active="1"');
            foreach ($friend_records as $frndKey => $frndValue)
            {
                $friendArr[] = $frndValue['user_id'];
            }

            //users created friend group
            $groupArr = $commonModel->getAllRecordDetail(TABLE_FRIEND_GROUPS, 'created_by= "' . $this->user['user_id'] . '"', 'group_name asc');

            $key = count($groupArr);
            $groupArr[$key]['group_id'] = '0';
            $groupArr[$key]['group_name'] = 'others';
            $groupArr[$key]['group_name'] = 'others';

            $this->view->bodyClass = "event-creator";
            $this->view->user_id = $this->user['user_id'];
            $msgArr = $this->messageModel->getUserMessages($this->user['user_id']);
            $msgAdapter = $this->messageModel->getUserMessages($this->user['user_id'], true, true);
            $sentMsgArr = $this->messageModel->getUserMessages($this->user['user_id'], false);
            $this->view->msgArr = $msgArr;
            //prd($msgArr);

            $adapter = new Zend_Paginator_Adapter_DbSelect($msgAdapter);
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(10);
            $this->view->paginator = $paginator;


            $mKey = "abcxyzinbox";
            $mustacheData = array();

            foreach ($msgArr as $mKey => $mValue)
            {

                $mustacheData['messages'][$mKey]['message_id'] = $mValue['message_id'];
                $mustacheData['messages'][$mKey]['message'] = $mValue['message_content'];
                $mustacheData['messages'][$mKey]['time'] = getTimeAgo(strtotime($mValue['message_timestamp']));
                $mustacheData['messages'][$mKey]['name'] = $mValue['user_username'];
                $mustacheData['messages'][$mKey]['receiver_id'] = $mValue['message_from'];

                $mustacheData['messages'][$mKey]['notadmin'] = $mValue['message_from'] == '0' ? false : true;


                $mustacheData['messages'][$mKey]['user_id'] = $this->user['user_id'];

                $mustacheData['messages'][$mKey]['is_inbox'] = true;
                $mustacheData['messages'][$mKey]['read'] = $mValue['message_read'] ? 'white-bg' : '';

                $mustacheData['messages'][$mKey]['delete-link'] = $this->view->baseUrl() . "/ajax/deletemessage/id/" . $mValue['message_id'] . "/inbox/1";

                $mustacheData['messages'][$mKey]['avatar'] = is_file(ROOT_PATH . "/" . MEMBER_IMAGE_PATH . "/" . $mValue['user_id'] . ".jpg") ? $this->view->baseUrl(MEMBER_IMAGE_PATH . "/" . $mValue['user_id'] . ".jpg") : $this->view->baseUrl(MEMBER_NO_IMAGE_FILE);

                //new
                $mustacheData['messages'][$mKey]['user_bio'] = $mValue['user_bio'];
                $mustacheData['messages'][$mKey]['friend_name'] = $mValue['user_first_name'] . " " . $mValue['user_last_name'];
                $mustacheData['messages'][$mKey]['dob'] = date("d F Y", strtotime($mValue['user_dob']));
                $mustacheData['messages'][$mKey]['user_from'] = $mValue['user_city'];
                $mustacheData['messages'][$mKey]['facebook_url'] = getFacebookUsernameUrl($mValue['facebook_username']);
                $mustacheData['messages'][$mKey]['twitter_url'] = getTwitterUsernameUrl($mValue['twitter_username']);
                $mustacheData['messages'][$mKey]['linkedin_url'] = getLinkedinUsernameUrl($mValue['linkedin_username']);
                $mustacheData['messages'][$mKey]['encrypted_user_id'] = getEncryptedString($mValue['message_from']);
                $mustacheData['messages'][$mKey]['add_friend_txt'] = !in_array($mValue['user_id'], $friendArr) ? "Add as Friend" : "Add to another group";
                
                $mustacheData['messages'][$mKey]['key'] = $mKey;
                
                $ugroupArr = $this->view->getHelper('Profile')->getFriendGroup($this->user['user_id'], $mValue['message_from']);

                $ugroup_array = array();
                foreach ($ugroupArr as $ugpval)
                {

                    $ugroup_array[] = $ugpval['group_id'];
                }

                //pr($ugroup_array);
                $_gcounter = 0;
                foreach ($groupArr as $gKey => $gValue)
                {

                    //echo $gValue['group_id']."<br>";
                    if (in_array($gValue['group_id'], $ugroup_array))
                        continue;

                    $mustacheData['messages'][$mKey]['group'][$_gcounter]['group_id'] = $gValue['group_id'];
                    $mustacheData['messages'][$mKey]['group'][$_gcounter]['group_name'] = $gValue['group_name'];
                    $mustacheData['messages'][$mKey]['group'][$_gcounter]['total_group_members'] = $this->view->getHelper('Profile')->getGroupMembersCount($this->user['user_id'], $gValue['group_id']);
                    $_gcounter++;
                }
                //prd($mustacheData['messages'][$mKey]['group']);
                //die;
            }
            //prd($mustacheData);

            $this->view->mustacheData = !empty($mustacheData) ? json_encode($mustacheData) : json_encode(array('nomessage' => true));
            $this->view->inboxCount = json_encode(array('inboxCount' => count($mustacheData['messages'])));


            $this->view->sentMsgArr = $sentMsgArr;

            $sentMsgAdapter = $this->messageModel->getUserMessages($this->user['user_id'], false, true);

            $adapter = new Zend_Paginator_Adapter_DbSelect($sentMsgAdapter);
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(10);
            $this->view->paginator1 = $paginator;


            $mustacheData = array();
            foreach ($sentMsgArr as $mKey => $mValue)
            {

                $mustacheData['messages'][$mKey]['message_id'] = $mValue['message_id'];
                $mustacheData['messages'][$mKey]['message'] = $mValue['message_content'];
                $mustacheData['messages'][$mKey]['time'] = getTimeAgo(strtotime($mValue['message_timestamp']));
                $mustacheData['messages'][$mKey]['name'] = $mValue['user_username'];
                $mustacheData['messages'][$mKey]['receiver_id'] = $mValue['message_to'];
                $mustacheData['messages'][$mKey]['user_id'] = $this->user['user_id'];
                $mustacheData['messages'][$mKey]['is_inbox'] = false;
                $mustacheData['messages'][$mKey]['delete-link'] = $this->view->baseUrl() . "/ajax/deletemessage/id/" . $mValue['message_id'] . "/outbox/1";
                $mustacheData['messages'][$mKey]['avatar'] = is_file(ROOT_PATH . "/" . MEMBER_IMAGE_PATH . "/" . $mValue['user_id'] . ".jpg") ? $this->view->baseUrl(MEMBER_IMAGE_PATH . "/" . $mValue['user_id'] . ".jpg") : $this->view->baseUrl(MEMBER_NO_IMAGE_FILE);
                $mustacheData['messages'][$mKey]['user_bio'] = $mValue['user_bio'];

                $mustacheData['messages'][$mKey]['notadmin'] = $mValue['message_from'] == '0' ? false : true;
                //new
                $mustacheData['messages'][$mKey]['user_bio'] = $mValue['user_bio'];
                $mustacheData['messages'][$mKey]['friend_name'] = $mValue['user_first_name'] . " " . $mValue['user_last_name'];
                $mustacheData['messages'][$mKey]['dob'] = date("d F Y", strtotime($mValue['user_dob']));
                $mustacheData['messages'][$mKey]['user_from'] = $mValue['user_city'];
                $mustacheData['messages'][$mKey]['facebook_url'] = getFacebookUsernameUrl($mValue['facebook_username']);
                $mustacheData['messages'][$mKey]['twitter_url'] = getTwitterUsernameUrl($mValue['twitter_username']);
                $mustacheData['messages'][$mKey]['linkedin_url'] = getLinkedinUsernameUrl($mValue['linkedin_username']);
                $mustacheData['messages'][$mKey]['encrypted_user_id'] = getEncryptedString($mValue['message_to']);
                $mustacheData['messages'][$mKey]['add_friend_txt'] = !in_array($mValue['user_id'], $friendArr) ? "Add as Friend" : "Add to another group";
                $mustacheData['messages'][$mKey]['key'] = $mKey;
                $ugroupArr = $this->view->getHelper('Profile')->getFriendGroup($this->user['user_id'], $mValue['message_to']);

                $__counter = 0;
                foreach ($groupArr as $gKey => $gValue)
                {
                    if (in_array_custom($gValue['group_id'], $ugroupArr, 'group_id'))
                        continue;

                    $mustacheData['messages'][$mKey]['group'][$__counter]['group_id'] = $gValue['group_id'];
                    $mustacheData['messages'][$mKey]['group'][$__counter]['group_name'] = $gValue['group_name'];
                    $mustacheData['messages'][$mKey]['group'][$__counter]['total_group_members'] = $this->view->getHelper('Profile')->getGroupMembersCount($this->user['user_id'], $gValue['group_id']);

                    $__counter++;
                }
            }
//            prd($mustacheData);
//            die;
            $this->view->omustacheData = !empty($mustacheData) ? json_encode($mustacheData) : json_encode(array("nomessage" => true));
            $this->view->outboxCount = json_encode(array('outboxCount' => count($mustacheData['messages']), 'function_name' => 'lytebox'));
            //prd($msgArr);
        }

        function replymessageAction()
        {
            $msg = array();
            $params = $this->_getAllParams();

            $result = $this->messageModel->sendMessage($params['message_to'], false, $params['commbox']);

            if ($result > 0)
            {
                //$msgResult = $this->messageModel->getMessagebyId($result);
                $msg['success'] = true;
                $msg['id'] = $result;
                //$msg['message_content'] = $msgResult[''];
                $msg['message'] = 'Message successfully sent';
            }
            else
            {
                $msg['error'] = true;
                $msg['message'] = 'some error occurred';
            }


            echo json_encode($msg);
            //echo $msg;
        }

        public function ajaxAction()
        {

            $this->_helper->layout()->disableLayout();
            gcm($this->_helper->getHelper('viewRenderer')->setNoRender);
            die;
        }

    }

?>
